<?php

/**
 * lib.php
 * 
 */
/* * */
function redirection($url) {
    print $url;
    if (!empty($url)) {
        Header("HTTP/1.1 301 Moved Permanently");
        Header("Location: $url");
        exit;
    }
}

function begin_cache($file) {
    // if cache file exist and enough fresh, redirection the cache file 
    clearstatcache();
    $cachename = "cache_" . basename($file) . ".html";
    $spit = '/';
    if (strpos($file, '/') == false)
        $spit = '\\';
    $cachename_all = dirname($file) . $spit . $cachename;
    if (file_exists($cachename_all)) {
        // redirection policy
        redirection($cachename);
    }
    ob_start();
}

function end_cache($file) {
    $s = ob_get_contents();

    // output $s to cache file
    $cachename = "cache_" . basename($file) . ".html";
    $spit = '/';
    if (strpos($file, '/') == false)
        $spit = '\\';
    $cachename_all = dirname($file) . $spit . $cachename;

    $h = fopen($cachename_all, "w+");
    fwrite($h, $s);
    fclose($h);

    ob_end_flush();
}

function promotion($promotionname) {
    $promotionname .= "Promotion";
    require_once ("promotion/$promotionname.php");
    return single($promotionname);
}

function promotions() {
    $dir = dirname(__FILE__) . "/promotion/";
    $handle = opendir($dir);
    $res = array();
    while (($filename = readdir($handle)) !== false) {
        if (preg_match('/Promotion\.php/', $filename)) {
            require_once ($dir . $filename);
            $res [] = substr($filename, 0, -1 * strlen('promotion.php'));
        }
    }

    closedir($handle);
    return $res;
}

function get_db() {
    global $config;
    require_once 'db.php';
    extract($config['db_config']);
    static $singledb;
    if (empty($singledb))
        $singledb = new DB($host, $db, $user, $password, null, $charset);
    return $singledb;
}

function form($id, $action, $ajax = false, $ajaxCallback = 'null', $ajaxResponeTarget = 'null', $class = '') {
    require_once 'form.php';
    return new Form($id, $action, $ajax, $ajaxCallback, $ajaxResponeTarget, $class);
}

function single($class) {
    static $objs;
    if (isset($objs[$class]))
        return $objs[$class];
    return $objs[$class] = new $class();
}

function session_cache($name, $value) {
    $_SESSION["session_cache.$name"] = serialize($value);
}

function get_session_cache($name) {
    if (!isset($_SESSION["session_cache.$name"]))
        return null;
    return unserialize($_SESSION["session_cache.$name"]);
}

function get_url($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $content = curl_exec($ch);
    return $content;
}

/*函数返回带参数的url地址。
 * $paras_array传入key，value数组。
 * $paras_array将生成key1=value1&key2=value2形式
 * 与前面url合并作为链接地址
 * a=index没有特殊处理，也需传进$paras_array数组里。
 */
function get_cur_uri($paras_array)
{
    $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], '?') ? "": "?");
    $para = parse_url($url);
    //$para = geturl($url);
    if(isset($para['query']))
    {
        //parse_str($para['query'], $parse_para);
        //print_r($parse_para);
        
        if(!empty($paras_array))
        {
            $url = $para['path'].'?'.http_build_query($paras_array);
        }
    }
    
    return $url;
}

?>
